package com.bqtv.sport.dialog

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.bqtv.sport.adapter.SelectCityAdapter
import com.bqtv.sport.bean.ProvinceBean
import com.bqtv.sport.databinding.DialogSelectCityBinding
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hjq.base.BaseDialog
import com.hjq.base.BottomSheetDialog
import java.io.IOException


class SelectCityDialog {
    class Builder(context: Context) : BaseDialog.Builder<Builder>(context) {
        lateinit var cityAdapter: SelectCityAdapter
        lateinit var areaAdapter: SelectCityAdapter
        lateinit var provinceAdapter: SelectCityAdapter
        var provinceBean: ProvinceBean? = null
        var cityBean: ProvinceBean? = null
        var areaBean: ProvinceBean? = null

        var binding: DialogSelectCityBinding =
            DialogSelectCityBinding.inflate(
                LayoutInflater.from(context),
                FrameLayout(context),
                false
            )

        init {
            setContentView(binding.root)
            setData()
        }

        fun setData(): Builder {
            var listProvinceBean: MutableList<ProvinceBean>? = null
            try {
                // 从assets读取文件
                val inputStream = getContext().getAssets().open("china_address.json")
                val size = inputStream.available()
                val buffer = ByteArray(size)
                inputStream.read(buffer)
                inputStream.close()
                // 转换为字符串
                val json = String(buffer, charset("UTF-8"))
                // 使用Gson解析JSON
                val gson = Gson()
                val listType = object : TypeToken<MutableList<ProvinceBean?>?>() {}.getType()
                listProvinceBean = gson.fromJson(json, listType)
            } catch (e: IOException) {
                e.printStackTrace()
            }

            binding.rcvAddress.layoutManager = LinearLayoutManager(getContext())
            provinceAdapter = SelectCityAdapter()
            cityAdapter = SelectCityAdapter()
            areaAdapter = SelectCityAdapter()
            binding.rcvAddress.adapter = provinceAdapter
            provinceAdapter.submitList(listProvinceBean)

            binding.tvProvince.setTextColor(Color.parseColor("#03DB6C"))
            binding.vProvince.visibility = View.VISIBLE

            provinceAdapter.setOnItemClickListener { adapter, view, position ->
                provinceBean = provinceAdapter?.getItem(position)
                provinceAdapter.checkPosition = position
                provinceAdapter.notifyDataSetChanged()
                cityAdapter.checkPosition = 0
                cityAdapter.submitList(provinceBean?.cityList)
                binding.rcvAddress.adapter=cityAdapter
                binding.tvProvince.text=provinceBean?.name
                setTitle()
                binding.tvCity.setTextColor(Color.parseColor("#03DB6C"))
                binding.vCity.visibility = View.VISIBLE
            }

            cityAdapter.setOnItemClickListener { adapter, view, position ->
                cityBean = cityAdapter?.getItem(position)
                cityAdapter.checkPosition = position
                cityAdapter.notifyDataSetChanged()
                areaAdapter.checkPosition = 0
                binding.rcvAddress.adapter=areaAdapter
                areaAdapter.submitList(cityBean?.areaList)
                binding.tvCity.text=cityBean?.name
                setTitle()
                binding.tvArea.setTextColor(Color.parseColor("#03DB6C"))
                binding.vArea.visibility = View.VISIBLE
            }

            areaAdapter.setOnItemClickListener { adapter, view, position ->
                areaBean = areaAdapter?.getItem(position)
                areaAdapter.checkPosition = position
                areaAdapter.notifyDataSetChanged()
                binding.tvArea.text=areaBean?.name

                clickResult?.onAddressClick(provinceBean!!, cityBean!!, areaBean!!)
                dismiss()
            }
            setOnClickListener(binding.ivClose, binding.llProvince, binding.llCity, binding.llArea)
            return this
        }

        override fun onClick(view: View) {
            super.onClick(view)

            when (view) {
                binding.ivClose -> {
                    dismiss()
                }

                binding.llProvince -> {
                    setTitle()
                }

                binding.llCity -> {

                }

                binding.llArea -> {

                }

            }
        }

        fun setTitle() {
            binding.tvProvince.setTextColor(Color.parseColor("#222222"))
            binding.vProvince.visibility = View.INVISIBLE
            binding.tvCity.setTextColor(Color.parseColor("#222222"))
            binding.vCity.visibility = View.INVISIBLE
            binding.tvArea.setTextColor(Color.parseColor("#222222"))
            binding.vArea.visibility = View.INVISIBLE
        }

        override fun createDialog(context: Context, themeId: Int): BaseDialog {
            val dialog = BottomSheetDialog(context, themeId)
            dialog.getBottomSheetBehavior().setSkipCollapsed(true)
            return dialog
        }

        var clickResult: ClickResult? = null

        interface ClickResult {
            fun onAddressClick(provinceBean: ProvinceBean, cityBean:ProvinceBean, areaBean:ProvinceBean)
        }
    }
}